package Components;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JDialog;

import Starter.BroadcastListener;
import Starter.Menu;

import Connector.Connector;

import smash_tris.Room;

public class GameWindow extends JDialog implements KeyListener{
	
	/* Wa�ne elementy */
	private Room room;
	private TetrisPlayground playground;
	private GameOptionPanel GOP;
	private Connector connector;
	private Menu motherMenu;
	private BroadcastListener broadcastListener = null;
	
	/* Info Menu */
	private int width = 1000;
	private int heigh = 451;
	private boolean isFirst = false;
	
	//public GameWindow(Connector connector, Room room, Menu menu){
	public GameWindow(Connector connector, Room room){
		
		this.connector = connector;
		this.room = room;
		
		//this.motherMenu = menu;
		this.setVisible(true);
		this.setSize(width , heigh);
		this.setLayout(null);
		this.setResizable(false);
		this.broadcastListener = broadcastListener;
		initComponents();
		addKeyListener(this);
		this.addWindowListener(new MyWindowListener());

		
	}
	
	public void setFirst(){
		this.isFirst = true;
	}
	
	
	
	private void initComponents() {
			
			playground = new TetrisPlayground(this,width - 200, heigh);
			playground.setBounds(0, 0, width - 204, heigh);			
			this.add(playground);
									
			System.out.println("GameWindow:\t" +"initComponents" );
			GOP = new GameOptionPanel(this);
			GOP.setBounds(width - 204, 0, 202, heigh);
			this.add(GOP);

			GOP.setStart();
			
	}
	
	
	public void closeWindow(){
		
		
		this.getTetrisPlayground().getEnemy().stop();
		this.getTetrisPlayground().getTetris().stop();
		//this.deleteRoom();
	}
	
	public Connector getConnection(){
		return this.connector;
	}
	
	
	
	public Room getRoom() {
		return this.room;
	}
	
	public GameOptionPanel getGOP() {
		return this.GOP;
	}
	
	public TetrisPlayground getTetrisPlayground() {
		return this.playground;
	}

	@Override
	public void keyPressed(KeyEvent evt) {
		playground.getMyTetris().keyDown(evt);
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	} 
	

	private class MyWindowListener implements WindowListener {

		@Override
		public void windowActivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosed(WindowEvent e) {
			System.out.println("CLOSED");
			if (isFirst){
				GameWindow g = (GameWindow) e.getSource();
				g.closeWindow();
				isFirst = false;
			}
		}

		@Override
		public void windowClosing(WindowEvent e) {
			System.out.println("CLOSED");
			if (isFirst){
				GameWindow g = (GameWindow) e.getSource();
				g.closeWindow();
				
			}
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}

}
